using System;
using System.Reflection;

namespace TestCases.Utils
{
    public static class ReflectionUtils
    {

        public static TResult InvokeMethod<TResult>(this object target, Type type, string methodName, params object[] arguments)
        {
            return (TResult) type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, target, arguments);
        }
        
        public static TPropType GetProperty<TPropType>(this object target, Type type, string propertyName, params object[] indexes)
        {
            PropertyInfo property = type.GetProperty(propertyName);
            if (property != null)
            {
                return (TPropType) property.GetValue(target, indexes);
            }
            throw new TargetException("<{Target}> doesn't have <{Property}> property.".
                FormatWith(new {Target = target, Property = propertyName}));
        }
    }
}